package com.yvtian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yvtian.admin.dao.SysUserDao;
import com.yvtian.admin.pojo.SysMenu;
import com.yvtian.admin.dao.SysMenuDao;
import com.yvtian.admin.pojo.SysRole;
import com.yvtian.admin.pojo.SysRoleMenu;
import com.yvtian.admin.service.SysMenuService;
import com.yvtian.admin.service.SysRoleMenuService;
import com.yvtian.admin.vo.TreeVO;
import com.yvtian.utils.TreeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * (SysMenu)表服务实现类
 *
 * @author long
 * @since 2024-03-22 10:56:24
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuDao, SysMenu> implements SysMenuService {

    @Resource
    private SysMenuDao sysMenuDao;
    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    @Resource
    private SysUserDao userDao;

    @Override
    public List<SysMenu> getList(SysMenu sysMenu) {
        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(SysMenu::getIsDelete, 0);
        if (sysMenu.getParentId() != null) {
            queryWrapper.eq(SysMenu::getParentId, sysMenu.getParentId());
        }
        queryWrapper.orderByAsc(SysMenu::getMenuSort);

        return this.list(queryWrapper);
    }

    @Override
    public SysMenu getOne(int id) {
        SysMenu sysMenu = this.getById(id);
        if (sysMenu.getParentId() != -1) {
            SysMenu sysMenuP = this.getById(sysMenu.getParentId());
            sysMenu.setParentName(sysMenuP.getMenuName());
        }
        return sysMenu;
    }

    @Override
    public List<SysRoleMenu> listByRoleId(int id) {
        LambdaQueryWrapper<SysRoleMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRoleMenu::getRoleId,id);
        return sysRoleMenuService.list(queryWrapper);
    }

    @Override
    public List<SysMenu> listByUserId(String userId) {
        List<SysMenu> reqData = new ArrayList<>();
        //查询角色
        ArrayList<SysRole> tbRoles = userDao.findRoleByUserId(userId);
        if (tbRoles.size() == 0){
            return reqData;
        }
        List<Integer> roleIds = new ArrayList<>();
        for (SysRole tbRole : tbRoles) {
            roleIds.add(tbRole.getId());
        }

        LambdaQueryWrapper<SysRoleMenu> sysRoleMenuQueryWrapper = new LambdaQueryWrapper<>();
        sysRoleMenuQueryWrapper.select(SysRoleMenu::getMenuId);
        sysRoleMenuQueryWrapper.in(SysRoleMenu::getRoleId,roleIds);
        sysRoleMenuQueryWrapper.groupBy(SysRoleMenu::getMenuId);
        List<SysRoleMenu> sysRoleMenuList = sysRoleMenuService.list(sysRoleMenuQueryWrapper);
        if (sysRoleMenuList.isEmpty()){
            return reqData;
        }
        List<Integer> roleMenuIds = new ArrayList<>();
        for (SysRoleMenu sysRoleMenu : sysRoleMenuList) {
            roleMenuIds.add(sysRoleMenu.getMenuId());
        }

        //查询菜单
        LambdaQueryWrapper<SysMenu> sysMenuQueryWrapper = new LambdaQueryWrapper<>();
        sysMenuQueryWrapper.in(SysMenu::getId,roleMenuIds);
        sysMenuQueryWrapper.orderByAsc(SysMenu::getMenuSort);
        sysMenuQueryWrapper.orderByAsc(SysMenu::getId);

        return this.list(sysMenuQueryWrapper);
    }

    @Override
    public boolean delData(int id) {
        //删除菜单
        LambdaUpdateWrapper<SysMenu> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(SysMenu::getIsDelete, 1);
        updateWrapper.eq(SysMenu::getId, id)
                .or().eq(SysMenu::getParentId, id);

        //删除角色的对应的菜单
        LambdaQueryWrapper<SysRoleMenu> sysRoleMenuQueryWrapper = new LambdaQueryWrapper<>();
        sysRoleMenuQueryWrapper.eq(SysRoleMenu::getMenuId,id);
        sysRoleMenuService.remove(sysRoleMenuQueryWrapper);
        return this.update(updateWrapper);
    }

    @Override
    public boolean saveData(SysMenu sysMenu) {

        sysMenu.setCreateTime(new Date());
        sysMenu.setIsDelete(0);
        sysMenu.setIsDisable(0);
        return this.save(sysMenu);
    }

    @Override
    public List<SysMenu> getListTree(SysMenu sysMenu) {
        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(SysMenu::getIsDelete, 0);
        if (sysMenu.getParentId() != null) {
            queryWrapper.eq(SysMenu::getParentId, sysMenu.getParentId());
        }
        List<SysMenu> list = this.list(queryWrapper);
        List<TreeVO> treeVOS = new ArrayList<>();
        for (SysMenu menu : list) {
            TreeVO treeVO = new TreeVO();
            treeVO.setId(menu.getId());
            treeVO.setLabel(menu.getMenuName());
        }

        return null;
    }
}
